﻿namespace Deppton.Mobile.Views
{
    using System;
    using System.Globalization;

    public partial class MainForm : DepptonForm
    {
        public MainForm(IServicesPortfolio services, Func<CredentialsController> credentialsControllerFactory)
        {
            this.ServicesPortfolio = services;
            this.CredentialsControllerFactory = credentialsControllerFactory;
            this.InitializeComponent();
            this.Load += new EventHandler(this.FormLoad);
        }

        private IServicesPortfolio ServicesPortfolio
        {
            get;
            set;
        }

        private Func<CredentialsController> CredentialsControllerFactory
        {
            get;
            set;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            this.UpdateInformation();
        }

        private void SynchronizeActionMenuItemClick(object sender, EventArgs e)
        {
            string userName;
            string password;

            if (this.CredentialsControllerFactory.Invoke().GetCredentials(out userName, out password))
            {
                this.ServicesPortfolio.SynchronizationService.Synchronize(userName, password, this.ServicesPortfolio.SurveysStorageService);
                this.UpdateInformation();
            }
        }

        private void UpdateInformation()
        {
            this.UserNameLabel.Text = this.ServicesPortfolio.UserProfileService.UserProfile.UserName;
            this.RoleLabel.Text = this.ServicesPortfolio.UserProfileService.UserProfile.Role;
        }

        private void WorkItemsActionsMenuItemClick(object sender, EventArgs e)
        {
            using (var workQueueForm = new WorkQueueForm(this.ServicesPortfolio))
            {
                workQueueForm.ShowDialog();
            }
        }
    }
}
